home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxclipm.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  11.2 KB  |  367 lines

  1. /* Copyright (C) 1998, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxclipm.c,v 1.3 2000/09/19 19:00:34 lpd Exp $ */
  20. /* Mask clipping device */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gsbittab.h"
  24. #include "gxdevice.h"
  25. #include "gxdevmem.h"
  26. #include "gxclipm.h"
  27.  
  28. /* Device procedures */
  29. private dev_proc_fill_rectangle(mask_clip_fill_rectangle);
  30. private dev_proc_copy_mono(mask_clip_copy_mono);
  31. private dev_proc_copy_color(mask_clip_copy_color);
  32. private dev_proc_copy_alpha(mask_clip_copy_alpha);
  33. private dev_proc_strip_tile_rectangle(mask_clip_strip_tile_rectangle);
  34. private dev_proc_strip_copy_rop(mask_clip_strip_copy_rop);
  35. private dev_proc_get_clipping_box(mask_clip_get_clipping_box);
  36.  
  37. /* The device descriptor. */
  38. const gx_device_mask_clip gs_mask_clip_device =
  39. {std_device_std_body_open(gx_device_mask_clip, 0, "mask clipper",
  40.               0, 0, 1, 1),
  41.  {gx_default_open_device,
  42.   gx_forward_get_initial_matrix,
  43.   gx_default_sync_output,
  44.   gx_default_output_page,
  45.   gx_default_close_device,
  46.   gx_forward_map_rgb_color,
  47.   gx_forward_map_color_rgb,
  48.   mask_clip_fill_rectangle,
  49.   gx_default_tile_rectangle,
  50.   mask_clip_copy_mono,
  51.   mask_clip_copy_color,
  52.   gx_default_draw_line,
  53.   gx_forward_get_bits,
  54.   gx_forward_get_params,
  55.   gx_forward_put_params,
  56.   gx_forward_map_cmyk_color,
  57.   gx_forward_get_xfont_procs,
  58.   gx_forward_get_xfont_device,
  59.   gx_forward_map_rgb_alpha_color,
  60.   gx_forward_get_page_device,
  61.   gx_forward_get_alpha_bits,
  62.   mask_clip_copy_alpha,
  63.   gx_forward_get_band,
  64.   gx_default_copy_rop,
  65.   gx_default_fill_path,
  66.   gx_default_stroke_path,
  67.   gx_default_fill_mask,
  68.   gx_default_fill_trapezoid,
  69.   gx_default_fill_parallelogram,
  70.   gx_default_fill_triangle,
  71.   gx_default_draw_thin_line,
  72.   gx_default_begin_image,
  73.   gx_default_image_data,
  74.   gx_default_end_image,
  75.   mask_clip_strip_tile_rectangle,
  76.   mask_clip_strip_copy_rop,
  77.   mask_clip_get_clipping_box,
  78.   gx_default_begin_typed_image,
  79.   gx_forward_get_bits_rectangle,
  80.   gx_forward_map_color_rgb_alpha,
  81.   gx_no_create_compositor,
  82.   gx_forward_get_hardware_params,
  83.   gx_default_text_begin,
  84.   gx_default_finish_copydevice
  85.  }
  86. };
  87.  
  88. /* Fill a rectangle by painting through the mask. */
  89. private int
  90. mask_clip_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  91.              gx_color_index color)
  92. {
  93.     gx_device_mask_clip *cdev = (gx_device_mask_clip *) dev;
  94.     gx_device *tdev = cdev->target;
  95.  
  96.     /* Clip the rectangle to the region covered by the mask. */
  97.     int mx0 = x + cdev->phase.x, my0 = y + cdev->phase.y;
  98.     int mx1 = mx0 + w, my1 = my0 + h;
  99.  
  100.     if (mx0 < 0)
  101.     mx0 = 0;
  102.     if (my0 < 0)
  103.     my0 = 0;
  104.     if (mx1 > cdev->tiles.size.x)
  105.     mx1 = cdev->tiles.size.x;
  106.     if (my1 > cdev->tiles.size.y)
  107.     my1 = cdev->tiles.size.y;
  108.     return (*dev_proc(tdev, copy_mono))
  109.     (tdev, cdev->tiles.data + my0 * cdev->tiles.raster, mx0,
  110.      cdev->tiles.raster, cdev->tiles.id,
  111.      mx0 - cdev->phase.x, my0 - cdev->phase.y,
  112.      mx1 - mx0, my1 - my0, gx_no_color_index, color);
  113. }
  114.  
  115. /*
  116.  * Clip the rectangle for a copy operation.
  117.  * Sets m{x,y}{0,1} to the region in the mask coordinate system;
  118.  * subtract cdev->phase.{x,y} to get target coordinates.
  119.  * Sets sdata, sx to adjusted values of data, sourcex.
  120.  * References cdev, data, sourcex, raster, x, y, w, h.
  121.  */
  122. #define DECLARE_MASK_COPY\
  123.     const byte *sdata;\
  124.     int sx, mx0, my0, mx1, my1
  125. #define FIT_MASK_COPY(data, sourcex, raster, vx, vy, vw, vh)\
  126.     BEGIN\
  127.       sdata = data, sx = sourcex;\
  128.       mx0 = vx + cdev->phase.x, my0 = vy + cdev->phase.y;\
  129.       mx1 = mx0 + vw, my1 = my0 + vh;\
  130.       if ( mx0 < 0 )\
  131.         sx -= mx0, mx0 = 0;\
  132.       if ( my0 < 0 )\
  133.         sdata -= my0 * raster, my0 = 0;\
  134.       if ( mx1 > cdev->tiles.size.x )\
  135.         mx1 = cdev->tiles.size.x;\
  136.       if ( my1 > cdev->tiles.size.y )\
  137.         my1 = cdev->tiles.size.y;\
  138.     END
  139.  
  140. /* Copy a monochrome bitmap by playing Boolean games. */
  141. private int
  142. mask_clip_copy_mono(gx_device * dev,
  143.         const byte * data, int sourcex, int raster, gx_bitmap_id id,
  144.             int x, int y, int w, int h,
  145.             gx_color_index color0, gx_color_index color1)
  146. {
  147.     gx_device_mask_clip *cdev = (gx_device_mask_clip *) dev;
  148.     gx_device *tdev = cdev->target;
  149.     gx_color_index color, mcolor0, mcolor1;
  150.  
  151.     DECLARE_MASK_COPY;
  152.     int cy, ny;
  153.     int code;
  154.  
  155.     setup_mask_copy_mono(cdev, color, mcolor0, mcolor1);
  156.     FIT_MASK_COPY(data, sourcex, raster, x, y, w, h);
  157.     for (cy = my0; cy < my1; cy += ny) {
  158.     int ty = cy - cdev->phase.y;
  159.     int cx, nx;
  160.  
  161.     ny = my1 - cy;
  162.     if (ny > cdev->mdev.height)
  163.         ny = cdev->mdev.height;
  164.     for (cx = mx0; cx < mx1; cx += nx) {
  165.         int tx = cx - cdev->phase.x;
  166.  
  167.         nx = mx1 - cx;    /* also should be min */
  168.         /* Copy a tile slice to the memory device buffer. */
  169.         memcpy(cdev->buffer.bytes,
  170.            cdev->tiles.data + cy * cdev->tiles.raster,
  171.            cdev->tiles.raster * ny);
  172.         /* Intersect the tile with the source data. */
  173.         /* mcolor0 and mcolor1 invert the data if needed. */
  174.         /* This call can't fail. */
  175.         (*dev_proc(&cdev->mdev, copy_mono)) ((gx_device *) & cdev->mdev,
  176.                      sdata + (ty - y) * raster, sx + tx - x,
  177.                          raster, gx_no_bitmap_id,
  178.                        cx, 0, nx, ny, mcolor0, mcolor1);
  179.         /* Now copy the color through the double mask. */
  180.         code = (*dev_proc(tdev, copy_mono)) (cdev->target,
  181.                  cdev->buffer.bytes, cx, cdev->tiles.raster,
  182.                          gx_no_bitmap_id,
  183.                   tx, ty, nx, ny, gx_no_color_index, color);
  184.         if (code < 0)
  185.         return code;
  186.     }
  187.     }
  188.     return 0;
  189. }
  190.  
  191. /*
  192.  * Define the run enumerator for the other copying operations.  We can't use
  193.  * the BitBlt tricks: we have to scan for runs of 1s.  There are obvious
  194.  * ways to speed this up; we'll implement some if we need to.
  195.  */
  196. private int
  197. clip_runs_enumerate(gx_device_mask_clip * cdev,
  198.             int (*process) (P5(clip_callback_data_t * pccd, int xc, int yc, int xec, int yec)),
  199.             clip_callback_data_t * pccd)
  200. {
  201.     DECLARE_MASK_COPY;
  202.     int cy;
  203.     const byte *tile_row;
  204.     gs_int_rect prev;
  205.     int code;
  206.  
  207.     FIT_MASK_COPY(pccd->data, pccd->sourcex, pccd->raster,
  208.           pccd->x, pccd->y, pccd->w, pccd->h);
  209.     tile_row = cdev->tiles.data + my0 * cdev->tiles.raster + (mx0 >> 3);
  210.     prev.p.y = prev.q.y = -1;    /* arbitrary */
  211.     for (cy = my0; cy < my1; cy++) {
  212.     int cx = mx0;
  213.     const byte *tp = tile_row;
  214.  
  215.     if_debug1('B', "[B]clip runs y=%d:", cy - cdev->phase.y);
  216.     while (cx < mx1) {
  217.         int len;
  218.         int tx1, tx, ty;
  219.  
  220.         /* Skip a run of 0s. */
  221.         len = byte_bit_run_length[cx & 7][*tp ^ 0xff];
  222.         if (len < 8) {
  223.         cx += len;
  224.         if (cx >= mx1)
  225.             break;
  226.         } else {
  227.         cx += len - 8;
  228.         tp++;
  229.         while (cx < mx1 && *tp == 0)
  230.             cx += 8, tp++;
  231.         if (cx >= mx1)
  232.             break;
  233.         cx += byte_bit_run_length_0[*tp ^ 0xff];
  234.         if (cx >= mx1)
  235.             break;
  236.         }
  237.         tx1 = cx - cdev->phase.x;
  238.         /* Scan a run of 1s. */
  239.         len = byte_bit_run_length[cx & 7][*tp];
  240.         if (len < 8) {
  241.         cx += len;
  242.         if (cx > mx1)
  243.             cx = mx1;
  244.         } else {
  245.         cx += len - 8;
  246.         tp++;
  247.         while (cx < mx1 && *tp == 0xff)
  248.             cx += 8, tp++;
  249.         if (cx > mx1)
  250.             cx = mx1;
  251.         else {
  252.             cx += byte_bit_run_length_0[*tp];
  253.             if (cx > mx1)
  254.             cx = mx1;
  255.         }
  256.         }
  257.         tx = cx - cdev->phase.x;
  258.         if_debug2('B', " %d-%d,", tx1, tx);
  259.         ty = cy - cdev->phase.y;
  260.         /* Detect vertical rectangles. */
  261.         if (prev.p.x == tx1 && prev.q.x == tx && prev.q.y == ty)
  262.         prev.q.y = ty + 1;
  263.         else {
  264.         if (prev.q.y > prev.p.y) {
  265.             code = (*process)(pccd, tx1, ty, tx, ty + 1);
  266.             if (code < 0)
  267.             return code;
  268.         }
  269.         prev.p.x = tx1;
  270.         prev.p.y = ty;
  271.         prev.q.x = tx;
  272.         prev.q.y = ty + 1;
  273.         }
  274.     }
  275.     if_debug0('B', "\n");
  276.     tile_row += cdev->tiles.raster;
  277.     }
  278.     if (prev.q.y > prev.p.y) {
  279.     code = (*process)(pccd, prev.p.x, prev.p.y, prev.q.x, prev.q.y);
  280.     if (code < 0)
  281.         return code;
  282.     }
  283.     return 0;
  284. }
  285.  
  286. /* Copy a color rectangle */
  287. private int
  288. mask_clip_copy_color(gx_device * dev,
  289.         const byte * data, int sourcex, int raster, gx_bitmap_id id,
  290.              int x, int y, int w, int h)
  291. {
  292.     gx_device_mask_clip *cdev = (gx_device_mask_clip *) dev;
  293.     clip_callback_data_t ccdata;
  294.  
  295.     ccdata.tdev = cdev->target;
  296.     ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  297.     ccdata.x = x, ccdata.y = y, ccdata.w = w, ccdata.h = h;
  298.     return clip_runs_enumerate(cdev, clip_call_copy_color, &ccdata);
  299. }
  300.  
  301. /* Copy a rectangle with alpha */
  302. private int
  303. mask_clip_copy_alpha(gx_device * dev,
  304.         const byte * data, int sourcex, int raster, gx_bitmap_id id,
  305.         int x, int y, int w, int h, gx_color_index color, int depth)
  306. {
  307.     gx_device_mask_clip *cdev = (gx_device_mask_clip *) dev;
  308.     clip_callback_data_t ccdata;
  309.  
  310.     ccdata.tdev = cdev->target;
  311.     ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  312.     ccdata.x = x, ccdata.y = y, ccdata.w = w, ccdata.h = h;
  313.     ccdata.color[0] = color, ccdata.depth = depth;
  314.     return clip_runs_enumerate(cdev, clip_call_copy_alpha, &ccdata);
  315. }
  316.  
  317. private int
  318. mask_clip_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  319.                    int x, int y, int w, int h,
  320.                    gx_color_index color0, gx_color_index color1,
  321.                    int phase_x, int phase_y)
  322. {
  323.     gx_device_mask_clip *cdev = (gx_device_mask_clip *) dev;
  324.     clip_callback_data_t ccdata;
  325.  
  326.     ccdata.tdev = cdev->target;
  327.     ccdata.x = x, ccdata.y = y, ccdata.w = w, ccdata.h = h;
  328.     ccdata.tiles = tiles;
  329.     ccdata.color[0] = color0, ccdata.color[1] = color1;
  330.     ccdata.phase.x = phase_x, ccdata.phase.y = phase_y;
  331.     return clip_runs_enumerate(cdev, clip_call_strip_tile_rectangle, &ccdata);
  332. }
  333.  
  334. private int
  335. mask_clip_strip_copy_rop(gx_device * dev,
  336.            const byte * data, int sourcex, uint raster, gx_bitmap_id id,
  337.              const gx_color_index * scolors,
  338.        const gx_strip_bitmap * textures, const gx_color_index * tcolors,
  339.              int x, int y, int w, int h,
  340.                int phase_x, int phase_y, gs_logical_operation_t lop)
  341. {
  342.     gx_device_mask_clip *cdev = (gx_device_mask_clip *) dev;
  343.     clip_callback_data_t ccdata;
  344.  
  345.     ccdata.tdev = cdev->target;
  346.     ccdata.x = x, ccdata.y = y, ccdata.w = w, ccdata.h = h;
  347.     ccdata.data = data, ccdata.sourcex = sourcex, ccdata.raster = raster;
  348.     ccdata.scolors = scolors, ccdata.textures = textures,
  349.     ccdata.tcolors = tcolors;
  350.     ccdata.phase.x = phase_x, ccdata.phase.y = phase_y, ccdata.lop = lop;
  351.     return clip_runs_enumerate(cdev, clip_call_strip_copy_rop, &ccdata);
  352. }
  353.  
  354. private void
  355. mask_clip_get_clipping_box(gx_device * dev, gs_fixed_rect * pbox)
  356. {
  357.     gx_device_mask_clip *cdev = (gx_device_mask_clip *) dev;
  358.     gx_device *tdev = cdev->target;
  359.     gs_fixed_rect tbox;
  360.  
  361.     (*dev_proc(tdev, get_clipping_box)) (tdev, &tbox);
  362.     pbox->p.x = tbox.p.x - cdev->phase.x;
  363.     pbox->p.y = tbox.p.y - cdev->phase.y;
  364.     pbox->q.x = tbox.q.x - cdev->phase.x;
  365.     pbox->q.y = tbox.q.y - cdev->phase.y;
  366. }
  367.